package com.forum.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.forum.dao.ItemDAO;
import com.forum.dao.PostDAO;
import com.forum.pojo.Post;
import com.forum.pojo.Subitem;
import com.forum.pojo.User;
import com.forum.util.BaseAction;
import com.opensymphony.xwork2.ActionContext;

@Component("postAction")
@Scope("prototype")
public class PostAction extends BaseAction{
	private Post post;
	private String title;
	private String item;
	private String content;
	private PostDAO postDAO;
	private String userName;
	private int userId;
	private User user;
	private Subitem subitem;
	private List<Post> al;
	private List<String> itemList;
	private int postid;
	private ItemDAO itemDAO;
	Calendar cal=Calendar.getInstance();
	
	
	/**
	 * 发帖
	 */
	public String execute() throws Exception{
		System.out.println("userId = "+userId+"userName = "+userName+"content = "+content+"item = "+item+"title = "+title);
		String postdate = cal.get(Calendar.YEAR)+"年"+cal.get(Calendar.MONTH)+"月"+cal.get(Calendar.DATE)+"日";
		try {
			user= new User();
			user.setUserId(userId);
			subitem = new Subitem();
			subitem.setItemname(item);
			subitem.setItemid(1);
			post = new Post();
			post.setContent(content);
			post.setPosttime(postdate);
			post.setHeadline(title);
			post.setUser(user);
			post.setSubitem(subitem);
			post.setReplynum(0);
			if(postDAO.addPost(post) <= 0){
				return ERROR;
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}
	
	

	/**
	 * 
	  * viewPost 方法 
	  * 方法说明:查看当前用户的所有帖子 
	  * @return 
	  * @return String 
	  * @author Evan 
	  * @date 2012-12-25
	 */
	public String viewPost(){
		HttpSession session=ServletActionContext.getRequest().getSession();            
		try {
			if(!postDAO.viewAllPost(userId).equals(null)){
				al = postDAO.viewAllPost(userId);
				session.setAttribute("PostList",al);
			}
			return "viewall";
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return "viewErr";
		}
	}
	
	/**
	 * 
	  * viewSubmite 方法 
	  * <p>方法说明:</p> 
	  * @return 
	  * @return String 
	  * @author Evan 
	  * @date 2012-12-28
	 */
	public String viewSubmite(){
		try {
			if(!itemDAO.retrieveItem_name().equals(null)){
				itemList = itemDAO.retrieveItem_name();
				this.getSession().put("PostList",itemList);
			}
			System.out.println(this.getSession().get("PostList"));
			return "viewSubmite";
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return "viewSubmiteErr";
	}
	
	/**
	 * 
	  * viewPostById 方法 
	  * 方法说明:根据postid查询Post
	  * @return 
	  * @return String 
	  * @author Evan 
	  * @date 2012-12-26
	 */
	public String viewPostById(){
		HttpSession session=ServletActionContext.getRequest().getSession(); 
		try {
			if(!postDAO.viewPostById(postid).equals(null)){
				post = postDAO.viewPostById(postid);
				session.setAttribute("Post",post);
			}
			return "viewpostbyid";
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return "viewpostbyidErr";
	}
	
	
	public List<String> getItemList() {
		return itemList;
	}
	public void setItemList(List<String> itemList) {
		this.itemList = itemList;
	}
	public ItemDAO getItemDAO() {
		return itemDAO;
	}
	public void setItemDAO(ItemDAO itemDAO) {
		this.itemDAO = itemDAO;
	}
	public int getPostid() {
		return postid;
	}
	public void setPostid(int postid) {
		this.postid = postid;
	}
	public List<Post> getAl() {
		return al;
	}
	public void setAl(List<Post> al) {
		this.al = al;
	}
	public Subitem getSubitem() {
		return subitem;
	}
	public void setSubitem(Subitem subitem) {
		this.subitem = subitem;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public Post getPost() {
		return post;
	}
	public void setPost(Post post) {
		this.post = post;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getItem() {
		return item;
	}
	public void setItem(String item) {
		this.item = item;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public PostDAO getPostDAO() {
		return postDAO;
	}
	@Resource
	public void setPostDAO(PostDAO postDAO) {
		this.postDAO = postDAO;
	}

}
